const articleModel = require('../model/articleModel')
const {
  SUCCESS_RESPONSE,
  FAIL_RESPONSE,
  PORT,
  HOST,
} = require('../config/constant')

// 修改文章
exports.changeAritcleData = async (req, res) => {
  let newArticleData = req.body
  await articleModel.changeArticle(newArticleData)
  res.json(SUCCESS_RESPONSE('修改文章成功'))
}

// 获取所编辑的文章数据
exports.getEditArticleData = async (req, res) => {
  let { article_id } = req.query
  let result = await articleModel.getEditArticle(article_id)
  result.cover = `${HOST}:${PORT}/` + result.cover
  res.json(SUCCESS_RESPONSE('获取所编辑的文章数据成功', result))
}

// 获取标签文章数据
exports.getTagArticleData = async (req, res) => {
  let result = await articleModel.getTagArticle()
  res.json(SUCCESS_RESPONSE('获取分类文章成功', result))
}

// 获取分类文章数据
exports.getCategoryArticleData = async (req, res) => {
  let result = await articleModel.getCategoryArticle()
  res.json(SUCCESS_RESPONSE('获取分类文章成功', result))
}

// 添加文章
exports.addArticleData = async (req, res) => {
  let newArticleData = req.body
  await articleModel.addArticle(newArticleData)
  res.json(SUCCESS_RESPONSE('添加文章成功'))
}

// 获取文章的数据和数量
exports.getArticleData = async (req, res) => {
  let { page, pagesize, title } = req.query
  let articleData = await articleModel.getArticle(page, pagesize, title)
  let articleCount = await articleModel.getCount(title)
  let server_url = `${HOST}:${PORT}/`
  res.json(
    SUCCESS_RESPONSE('获取文章数据成功', {
      articleData,
      articleCount,
      server_url,
    })
  )
}
